home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / applic / ncsa / Mac / Telnet2.6 / prerelease / d4 / Telnet2.6.1d4.src.sit.hqx / Telnet 2.6.1d4 source / source / init / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-21  |  12.3 KB  |  446 lines

  1. /*
  2. *    init.c
  3. *    Top level of init code & general initialization routines
  4. *    Called only by main()
  5. *
  6. *****************************************************************
  7. *    NCSA Telnet for the Macintosh                                *
  8. *                                                                *
  9. *    National Center for Supercomputing Applications                *
  10. *    Software Development Group                                    *
  11. *    152 Computing Applications Building                            *
  12. *    605 E. Springfield Ave.                                        *
  13. *    Champaign, IL  61820                                        *
  14. *                                                                *
  15. *    Copyright (c) 1986-1992,                                    *
  16. *    Board of Trustees of the University of Illinois                *
  17. *****************************************************************
  18. *  Revisions:
  19. *  7/92        Telnet 2.6:    Initial version.  Jim Browne
  20. */
  21.  
  22. #ifdef MPW
  23. #pragma segment INIT
  24. #endif
  25.  
  26. #include "TelnetHeader.h"
  27.  
  28. #include <string.h>
  29. #include <Folders.h>
  30. #include <Script.h>
  31. #include <stdio.h>
  32. #include <GestaltEqu.h>
  33. #include <AppleEvents.h>
  34.  
  35. #include "init.proto.h"
  36. #include "debug.h"
  37. #include "mainseg.proto.h"        /* For forcequit() proto */
  38. #include "iNetwork.proto.h"    /* For initnet proto */
  39. #include "macros.proto.h"        /* For initmacro proto */
  40. #include "wind.h"        /* For WindRec definition */
  41. #include "rsmac.proto.h"        /* For RSgetwindow & RSnewwindow protos */
  42. #include "DlogUtils.proto.h"    /* For VersionNumber, UItemAssign, GetNewMyDialog protos */
  43. #include "Sets.proto.h"        // For readconfig proto
  44. #include "prefs.proto.h"
  45. #include "vr.h"            /* For VRinit proto */
  46. #include "vsdata.h"
  47. #include "vsinterf.proto.h"    /* For VSinit proto */
  48. #include "vgtek.proto.h"        /* For VGinit proto */
  49. #include "vrrgmac.proto.h"    /* For MacRGinit proto */
  50. #include "telneterrors.h"        /* for DoError proto */
  51. #include "menuseg.proto.h"
  52. #include "translate.proto.h"
  53. #include "dnr.proto.h"    // For OpenResolver proto
  54. #include "tnae.h"
  55. #include "authencrypt.h"
  56. #include "authencrypt.proto.h"
  57. #include "ae.proto.h"            // For AppleEvent handlers
  58. #include "MacBinary.h"
  59. #include "binsubs.proto.h"        // For init_mb_files proto
  60. #include "drag.proto.h"            // For MyDragInit
  61.  
  62.  
  63. /*--------------------------------------------------------------------------------*/
  64. #define CurrentVersion 1                /* Last known SysEnvirons version */
  65. #define HFSPtr ((long *)1014)
  66.  
  67. extern    char *tempspot;                /* for temp storage only */
  68.  
  69. extern    Cursor *theCursors[];
  70.  
  71. extern    WindRec
  72.     *screens,        /* Window Records (VS) for :    Current Sessions */
  73.     *ftplog;            /*                                FTP Log             */
  74. extern    MenuHandle    myMenus[];
  75.  
  76. OSErr        io;
  77. char        *mungbuf;
  78. SysEnvRec    theWorld;                        /* System Environment record */
  79. short        TempItemsVRefNum;
  80. long        TempItemsDirID;
  81. Boolean        gKeyboardHasControlKey, gAEavail;
  82.  
  83. Boolean encryptOK;                // true if des encryption routines available
  84. Boolean authOK;                    // true if authentication driver available
  85.  
  86. /*    checkCONF - check if the user clicked on a configuration file */
  87. void checkCONF( void)
  88. {
  89.     short    i,mess,count;
  90.     AppFile    theFile;
  91.     FSSpec    FileSpec;
  92.     long    junk = 0;
  93.  
  94.     if (gAEavail) return;    // If AppleEvents are available, we'll get ODOCs
  95.  
  96. #ifndef __powerpc__
  97.     CountAppFiles(&mess,&count);
  98.     if (mess==1) return;
  99.     if (count<1) return;
  100.     for(i=1;i<=count;i++) {
  101.         GetAppFiles(i, &theFile);
  102.         if (theFile.fType=='CONF') {
  103.             ClrAppFiles(i);
  104.             BlockMove(&theFile.fName, FileSpec.name, (*theFile.fName)+1); // pstring copy theFile.fName -> FileSpec.name
  105.             GetWDInfo(theFile.vRefNum, &FileSpec.vRefNum, &FileSpec.parID, &junk);
  106.             readconfig(FileSpec);    /* BYU LSC */
  107.             }
  108.         }
  109. #endif
  110.     return;
  111. }
  112.  
  113. void initEvents( void)
  114. {
  115.     EventRecord myEvent;
  116.  
  117.     FlushEvents(everyEvent - diskEvt,0);    /* Don't Let 'em get off without seeing it. */
  118.  
  119.     WaitNextEvent(everyEvent, &myEvent, gApplicationPrefs->TimeSlice, 0L);    /* WNE always avail */
  120.     WaitNextEvent(everyEvent, &myEvent, gApplicationPrefs->TimeSlice, 0L);
  121. }
  122.  
  123. /* initftplog - initialize the FTP log window */
  124. void initftplog( void)
  125. {
  126.     Rect prect;
  127.  
  128.     SetRect(&prect, 300,256,512,384);
  129.  
  130.     ftplog = (WindRec *) NewPtrClear(sizeof(WindRec));
  131.     
  132.     ftplog->vs=RSnewwindow( &prect, 50, 80, 24,
  133.                     "\pFTP Log", 1, DefFONT, DefSIZE, gFTPServerPrefs->ShowFTPlog,
  134.                         1,0);    /* NCSA 2.5 */
  135.  
  136.     ftplog->wind = RSgetwindow( ftplog->vs);
  137.     ((WindowPeek)ftplog->wind)->windowKind = WIN_LOG;
  138.     
  139.     VSwrite(ftplog->vs,"\033[24;0H",7);        /* Move to bottom of screen */
  140.     ftplog->active=0;                         /* Meaningless */
  141.     ftplog->port=0;                            /* As is this */
  142.     ftplog->termstate=VTEKTYPE;
  143. }
  144.  
  145.   /* copies a specified cursor from a resource into a nonrelocatable block
  146.     and returns a pointer to it in *result. */
  147. void loadcursor( short CursorID, CursPtr *result)
  148. {
  149.     CursHandle tempcurs;
  150.     OSErr Err;
  151.  
  152.     *result = nil;
  153.     do /* once */
  154.       {
  155.         tempcurs = GetCursor(CursorID);
  156.         Err = ResError();
  157.         if (Err != noErr)
  158.             break;
  159.         *result = (CursPtr) NewPtr(sizeof(Cursor));        /* BYU LSC */
  160.         Err = MemError();
  161.         if (Err != noErr)
  162.             break;
  163.         LoadResource((Handle) tempcurs); /* in case it was purged */
  164.         Err = ResError();
  165.         if (Err != noErr)
  166.             break;
  167.         BlockMove((Ptr) *tempcurs, (Ptr) *result, sizeof(Cursor));
  168.       }
  169.     while (false);
  170.     if (Err != noErr)
  171.       {
  172.         if (*result != nil)
  173.             DisposPtr((Ptr) *result);
  174.         *result = &qd.arrow;
  175.       }
  176. } /* loadcursor */
  177.  
  178. /* initmcurs - Set up the mouse Cursors
  179.  *        NOTE: debug tells if the bug or the pointer is to be used as dflt.*/
  180. void initmcurs ( short debug) 
  181. {
  182.     loadcursor(rWatchCursor, &theCursors[watchcurs]);
  183.     loadcursor(rCrossCursor, &theCursors[graphcurs]);
  184.     loadcursor(rPOSCURS, &theCursors[poscurs]);
  185.     loadcursor(rBeamCursor, &theCursors[textcurs]);
  186.     loadcursor(rGINCURS, &theCursors[gincurs]);
  187.     loadcursor(rXferCURS, &theCursors[xfercurs]);
  188.     if (debug)
  189.         loadcursor(rDbugCURS, &theCursors[normcurs]);
  190.     else
  191.         theCursors[normcurs] = &qd.arrow;
  192.     SetCursor(theCursors[normcurs]);
  193. }
  194.  
  195. /*     If a file with the filetype "DBUG" was sent to us, set the debug mode. */
  196. short checkdebug( void)
  197. {
  198.     short i,mess,count;
  199.     AppFile theFile;
  200.     long junk = 0;
  201.  
  202.     if (gAEavail) return(FALSE);    //     Don't use CountAppFiles if AppleEvents
  203.                                     //    are available.
  204. #ifndef __powerpc__    
  205.     CountAppFiles(&mess,&count);
  206.     if (mess==1) return(FALSE);
  207.     if (count<1) return(FALSE);
  208.     for(i=1;i<=count;i++) {
  209.         GetAppFiles(i, &theFile);
  210.         if (theFile.fType=='DBUG') {
  211.             ClrAppFiles(i);
  212.             return(TRUE);
  213.             }
  214.         }
  215.     return(FALSE);
  216. #endif
  217. }
  218.  
  219. void InquireEnvironment( void)
  220. {
  221.     OSErr    err;
  222.     Boolean HFSflag;
  223.     long    response;
  224.     
  225.     long *HFSp=(long *)1014L;
  226.     
  227.     HFSflag= (*HFSp) > 0L;
  228.     if (!HFSflag) FatalAlert(NEED_HFS_ERR, 0, 0);
  229.  
  230.     err = SysEnvirons(CurrentVersion, &theWorld);
  231.     
  232.     if (err == envVersTooBig) FatalAlert(SYS_ENVIRON_ERR, 0, 0);
  233.     
  234.     if (theWorld.systemVersion < 0x0600) FatalAlert(SYSTEM_VERS_ERR, 0, 0);
  235.     
  236.     if (theWorld.machineType < 0) FatalAlert(ROM_VERS_ERR, 0, 0);
  237.     
  238.     // If there is a problem w/Gestalt, assume our keyboard has a Control key.
  239.     // Otherwise, we assume we have a control key unless a Mac or MacPlus keyboard is
  240.     // present.
  241.         
  242.     if ((err = Gestalt(gestaltKeyboardType, &response)) != noErr)
  243.         gKeyboardHasControlKey = TRUE;
  244.     else
  245.         gKeyboardHasControlKey = !(    (response == gestaltMacKbd) ||
  246.                                     (response == gestaltMacAndPad) ||
  247.                                     (response == gestaltMacPlusKbd) );
  248.                                     
  249.     /* System 6.0 and up ALWAYS has WNE trap! */
  250. }
  251.  
  252. #define    kURLEventClass    'GURL'
  253. #define    kGetURLEvent    'GURL'
  254.  
  255. void initmac( void)
  256. {
  257.     EventRecord myEvent;
  258.     short     i;
  259.     long    gestaltvalue;
  260.     OSErr    err;
  261.  
  262.     InitGraf(&qd.thePort);
  263.     InitFonts();
  264.     InitWindows();
  265.     InitMenus();
  266.     TEInit();
  267.     InitDialogs(0L);                /* No resume proc */
  268.     InitCursor();
  269.     
  270.     TelInfo = (TelInfoRec *)NewPtrClear(sizeof(TelInfoRec));
  271.     mungbuf = (char *)NewPtrClear(1024);
  272.     tempspot = (char *)NewPtrClear(256);
  273.     gApplicationPrefs = (ApplicationPrefs *)NewPtrClear(sizeof(ApplicationPrefs));
  274.     gFTPServerPrefs = (FTPServerPrefs *)NewPtrClear(sizeof(FTPServerPrefs));
  275.     screens = (WindRec *)NewPtrClear(MaxSess*sizeof(WindRec));
  276.  
  277.     InquireEnvironment();
  278.  
  279.     for (i=1;i<12;i++)
  280.         EventAvail( 0xffff, &myEvent);
  281.     
  282.     err = Gestalt(gestaltAppleEventsAttr, &gestaltvalue);        // See if AppleEvents are available
  283.     gAEavail = (!err && ((gestaltvalue >> gestaltAppleEventsPresent) & 0x0001));
  284.     
  285.     if (gAEavail) {
  286.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
  287.                                             MyHandleOAppUPP,0,FALSE)) != noErr)
  288.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  289.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
  290.                                             MyHandleODocUPP,0,FALSE)) != noErr)
  291.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  292.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,
  293.                                             MyHandlePDocUPP,0,FALSE)) != noErr)
  294.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  295.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
  296.                                             MyHandleQuitUPP,0,FALSE)) != noErr)
  297.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  298.         if ((err = AEInstallEventHandler(kURLEventClass,kGetURLEvent,
  299.                                             MyHandleGURLUPP,0,FALSE)) != noErr)
  300.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  301.         }
  302.  
  303.     TelInfo->screenRect = qd.screenBits.bounds;            /* well, they have to be set */
  304.                                                 /* somewhere, where else ? */
  305.     SetRect(&TelInfo->dragRect, 4, 24, TelInfo->screenRect.right-4,
  306.                         TelInfo->screenRect.bottom-4);
  307. }
  308.  
  309. void DoTheGlobalInits(void)
  310. {
  311.     long junk = 0;
  312.     ParamBlockRec pb;
  313.     
  314.     TelInfo->ScrlLock=0;
  315.     TelInfo->ftplogon=0;
  316.     TelInfo->done=0;
  317.     TelInfo->MacBinary=0;
  318.     TelInfo->numwindows=0;
  319.     TelInfo->CONFstate=0;
  320.     TelInfo->CONFactive=0;
  321.     TelInfo->SettingsFile=-1;
  322.     TelInfo->ApplicationFile = CurResFile();
  323.     TelInfo->ginon   = 0;
  324.     TelInfo->xferon  = 0;
  325.     TelInfo->graphs = 0;
  326.     TelInfo->debug = 1;
  327.     TelInfo->myfronttype = NO_WINDOW;
  328.     TelInfo->myfrontvs = 0;
  329.     TelInfo->suspended = FALSE;
  330.     TelInfo->myfrontwindow = 0L;    
  331.     TelInfo->myfrontRgn = 0L;    
  332.     TelInfo->lastCursor = 0L;
  333.     TelInfo->DefaultDirVRefNum = -1;
  334.     TelInfo->FTPClientVRefNum = -1;
  335.     TelInfo->DefaultDirDirID = 2;
  336.     TelInfo->FTPClientDirID = 2;
  337.     
  338.     pb.ioParam.ioNamePtr = 0L;
  339.     PBGetVol(&pb, FALSE);            /* see Tech Note 140 for implications of this */
  340.  
  341.     GetWDInfo(pb.ioParam.ioVRefNum, &(TelInfo->ApFolder.vRefNum), &(TelInfo->ApFolder.parID), &junk);
  342.     TelInfo->ApFolder.name[0] = 0;
  343.  
  344.     FindFolder( kOnSystemDisk, kSystemFolderType, kCreateFolder,
  345.                     &(TelInfo->SysFolder.vRefNum), &(TelInfo->SysFolder.parID));
  346.     TelInfo->SysFolder.name[0]=0;
  347.  
  348.     FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  349.                     &(TelInfo->PrefFolder.vRefNum), &(TelInfo->PrefFolder.parID));
  350.     TelInfo->PrefFolder.name[0]=0;
  351.     
  352.     FindFolder( kOnSystemDisk, kTemporaryFolderType, kCreateFolder, &TempItemsVRefNum, &TempItemsDirID);
  353. }
  354.  
  355. void init (void)
  356. {
  357.     DialogPtr     dtemp;        /* Used for dialog display */
  358.  
  359.     FlushEvents(everyEvent,0);
  360.     initmac();                /* initialize Macintosh stuff */
  361.     
  362.     DoTheGlobalInits();
  363.  
  364.     OpenPreferencesFile();
  365.     LoadPreferences();
  366.     UnloadSeg(&LoadPreferences);
  367.     
  368.     setupmenu(gApplicationPrefs->CommandKeys);
  369.  
  370.     dtemp = GetNewMyDialog(FirstDLOG, NULL, kInFront, (void *)ThirdCenterDialog);    /* opening dialog */
  371.     UItemAssign( dtemp, 2, VersionNumberUPP);
  372.     DrawDialog(dtemp);                                        /* while we init everything */
  373.  
  374.     trInit(myMenus[National]);    /* LU: Initialize translation routines; must be done */
  375.                                 /* LU: prior to checkdebug(), since even the debug      */
  376.                                 /* LU: output goes through the translation routines. */
  377.     UnloadSeg(&trInit);
  378.  
  379.     TelInfo->debug=checkdebug();        /* must return TRUE or FALSE */
  380.     
  381.     initmcurs(TelInfo->debug);        /* init the mouse cursor */
  382.     SetCursor(theCursors[watchcurs]);
  383.         
  384.     if (VSinit(MaxSess+2)) {        /* initialize the Virtual Screen */
  385.         DoError(104 | MEMORY_ERRORCLASS, LEVEL3, NULL);
  386.         forcequit();
  387.     }
  388.     UnloadSeg(&VSinit);
  389.     
  390.     InitDebug();
  391.     initftplog();                    /* allocate/initialize ftp log screen */
  392.  
  393.     VGinit();                        /* initialize Virtual Graphics after VS so that */
  394.                                     /* init messages can come on console screen */
  395.     UnloadSeg(&VGinit);
  396.     
  397.     VRinit();
  398.     UnloadSeg(&VRinit);
  399.     
  400.     MacRGinit();
  401.     UnloadSeg(&MacRGinit);
  402.     
  403.     init_mb_files();                /* Initialize file id's to closed state */
  404.     UnloadSeg(init_mb_files);
  405.  
  406.     initnet();                        /* initialize network stuff */
  407.     switchMenus(gApplicationPrefs->CommandKeys);
  408.  
  409.     TelInfo->done = FALSE;            // We're not quitting yet.
  410.  
  411.     SetCursor(theCursors[normcurs]);
  412.  
  413.     initmacros();
  414.     UnloadSeg(initmacros);
  415. //    initEvents();
  416.  
  417.     io = OpenResolver(NULL);
  418.  
  419.     if (io) {                                
  420.         DialogPtr theErrDialog;
  421.         short itemhit = 0;
  422.  
  423.         theErrDialog = GetNewDialog(RESOLVERERROR, NULL, kInFront);
  424.         DrawDialog(theErrDialog);
  425.         while (!itemhit)
  426.             ModalDialog(DLOGwOKUPP,&itemhit);
  427.         DisposDialog(theErrDialog);
  428.         }
  429.     
  430.     updateMenuChecks();
  431.     DoTheMenuChecks();
  432.  
  433.     MyDragInit();
  434.     UnloadSeg(MyDragInit);
  435.     
  436.     checkCONF();                     /* Did user click on a set? */
  437.  
  438.     DisposDialog(dtemp);            /* Remove the splashbox...  */
  439.  
  440.     encryptOK = authOK = true;
  441.     if (!auth_encrypt_init()) {
  442.         encryptOK = false;
  443.         authOK = false;
  444.     }
  445. }
  446.